var app = getApp();
var Moment = require("../../../utils/moment.js");
var DATE_LIST = [];
var DATE_YEAR = new Date().getFullYear();
var DATE_MONTH = new Date().getMonth() + 1;
var DATE_DAY = new Date().getDate();

Page({
    data: {
        homeArr:[],
        homeList:[],
        homeLength:"",
        pageCurrent:1,
        pageNum:"",  //总页数
        listName:"",
        typeList:"",
        typeId:"",
        typeVal: [],
        typeFlag: false,
        typePickerShow: false,
        styleList:"",
        styleId:"",
        styleVal: [],
        styleFlag: false,
        stylePickerShow: false,
        featureList:"",
        featureIndex:"",
        featureStr:"",
        featureArr:[],
        loading:false,   //"上拉加载中"
        loadingComplete:false,   //"加载完成"
        imgUrl:app.globalData.imgUrl,
        intimeStr:"",
        outtimeStr:"",
        flag:""

    },
    onLoad: function (e) {
        var that = this;
        var currentName=e.name;
        that.setData({
           navH: app.globalData.navHeight,
           marH: app.globalData.navHeight+20,
           listName: currentName,
           intimeStr:e.startDate,
           outtimeStr:e.endDate,
           flag:e.flag
        })
        //获取房源类型
        wx.request({
            url: app.globalData.apiHost + "/api/index/categoryList",
            data: {
                type: "scale"
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded' 
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var typeUnlimit = {id:0,name:'不限'};
                    var typeData = res.data.data;
                    typeData.unshift(typeUnlimit);
                    var typeObj=[];
                    typeObj.unshift(typeData);
                    that.setData({
                        typeList: typeObj
                    })
                }
            },
            
        });
        //获取风格
        wx.request({
            url: app.globalData.apiHost + "/api/index/categoryList",
            data: {
                type: "house_style"
            },
            header: {  
                'content-type': 'application/x-www-form-urlencoded' 
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var styleUnlimit = {id:0,name:'不限'};
                    var styleData = res.data.data;
                    styleData.unshift(styleUnlimit);
                    var styleObj=[];
                    styleObj.unshift(styleData);
                    that.setData({
                        styleList: styleObj
                    })
                }
            },
            
        });
        //获取特征
        wx.request({
            url: app.globalData.apiHost + "/api/index/categoryList",
            data: {
                type: "feature"
            },
            header: {  
                'content-type': 'application/x-www-form-urlencoded' 
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var featureData = res.data.data;
                    that.setData({
                        featureList: featureData
                    })
                }
            },
            
        });
    },
    onShow: function () {
        var that=this;
        let getDate = wx.getStorageSync("ROOM_SOURCE_DATE");
        this.setData({
            intimeStr: getDate.checkInDate,
            outtimeStr: getDate.checkOutDate,
            homeList:[],
            pageCurrent:1
        })
        //获取民宿列表
        that.homeStayList(that.data.styleId,that.data.typeId,that.data.featureStr,that.data.intimeStr,that.data.outtimeStr);
    },
    homeStayList: function(house_styleId,scaleId,featureStr,intimeStr,outtimeStr) {
        var that=this;
        wx.request({
            url: app.globalData.apiHost + "/api/index/houseList",
            data: {
                page: that.data.pageCurrent,
                pagesize:3,
                house_style:house_styleId,
                scale:scaleId,
                feature:featureStr,
                intime:intimeStr,
                outtime:outtimeStr,
                flag:that.data.flag
            },
            header: {  
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie':'token='+wx.getStorageSync('token')
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var listLength=res.data.data.data.length;
                    var listPageNum = res.data.data.last_page;
                    for (var i = 0; i < res.data.data.data.length; i++) { 
                        that.data.homeList.push(res.data.data.data[i]);
                    }
                    var houseData = that.data.homeList;
                    that.setData({
                        homeArr: houseData,
                        pageNum:listPageNum,
                        homeLength:listLength
                    })
                }
            },
            
        });
    },
    cartOperate:function(e){
        var that=this;
        var proId=e.currentTarget.dataset.id;
        var proIndex=e.currentTarget.dataset.key;
        //改变心愿单选择的状态
        if(that.data.homeArr[proIndex].cart_exists==1){
            that.data.homeArr[proIndex].cart_exists=0;
            app.cartDelete(proId,1);
        }else if(that.data.homeArr[proIndex].cart_exists==0){
            that.data.homeArr[proIndex].cart_exists=1;
            app.cartAdd(proId,1);
        }
        that.setData({
            homeArr:that.data.homeArr
       })
    },
    typePicker: function () {
        this.setData({
            typePickerShow: true
        })
    },
    sureCallBack_type(e) {
        var that=this;
        this.setData({
            typePickerShow: false,
            typeFlag:true,
            typeVal: e.detail.choosedData[0].name,
            typeId: e.detail.choosedData[0].id,
            homeList:[]
        })
        that.homeStayList(that.data.styleId,that.data.typeId,that.data.featureStr,that.data.intimeStr,that.data.outtimeStr);
    },
    cancleCallBack_type() {
        this.setData({
            typePickerShow: false,
        })
    },
    stylePicker: function () {
        this.setData({
            stylePickerShow: true
        })
    },
    sureCallBack_style(e) {
        var that=this;
        this.setData({
            stylePickerShow: false,
            styleFlag:true,
            styleVal: e.detail.choosedData[0].name,
            styleId: e.detail.choosedData[0].id,
            homeList:[]
        })
        that.homeStayList(that.data.styleId,that.data.typeId,that.data.featureStr,that.data.intimeStr,that.data.outtimeStr);
    },
    cancleCallBack_style() {
        this.setData({
            stylePickerShow: false,
        })
    },
    featurePicker(e){
       var that=this;
       var featureIdx=e.currentTarget.dataset.key;
       //获取特征id
       if((that.data.featureArr).indexOf(e.currentTarget.id)==-1){
          (that.data.featureArr).unshift(e.currentTarget.id);
       }else{
          var arrObjIdx=(that.data.featureArr).indexOf(e.currentTarget.id);
          (that.data.featureArr).splice(arrObjIdx,1);
       }
       var featureStr=(that.data.featureArr).toString();
       //改变特征选择的状态
       if(that.data.featureList[featureIdx].extend_text==true){
          that.data.featureList[featureIdx].extend_text=false;
       }else if(that.data.featureList[featureIdx].extend_text==false){
          that.data.featureList[featureIdx].extend_text=true;
       }
       that.setData({
            featureList:that.data.featureList,
            featureStr:featureStr,
            homeList:[]
       })
       that.homeStayList(that.data.styleId,that.data.typeId,that.data.featureStr,that.data.intimeStr,that.data.outtimeStr);
    },
    navBack: function () {
        wx.reLaunch({
            url: '../../index/index'
        })
    },
    onReachBottom: function () {
        let that = this;
        if (that.data.pageCurrent < that.data.pageNum) {
            that.setData({
                loading:true,
                loadingComplete:false,
                pageCurrent: that.data.pageCurrent + 1
            })
            that.homeStayList(that.data.styleId,that.data.typeId,'','','');
        }else if(that.data.pageCurrent >= that.data.pageNum){
            that.setData({
                loading:false,
                loadingComplete:true
            })
        }
    }
})
